package me.firstapp.service.bbs.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.utils.DateUtils;
import me.firstapp.common.utils.Page;
import me.firstapp.common.utils.StrUtils;
import me.firstapp.module.bbs.Advertisement;
import me.firstapp.repository.bbs.AdvertisementRepository;
import me.firstapp.service.bbs.AdvertisementService;

@Service
public class AdvertisementServiceImpl implements AdvertisementService {

	@Autowired
	private AdvertisementRepository advertisementRepository;

	@Override
	public List<Advertisement> findAvailableAds(Long apiClientId) {
		return advertisementRepository.findAvailableAds(apiClientId);
	}

	@Override
	public Page<Advertisement> findAdvertisementPage(Integer pageNo, Integer pageSize, Map<String, Object> searchParams,
			String sortParam, String sortType, Long apiClientId) {
		return advertisementRepository.findAdvertisementPage(pageNo, pageSize, searchParams, sortParam, sortType, apiClientId);
	}

	@Override
	public Advertisement addAdvertisement(String title, String description, String image, String url, Long startDate,
			Long endDate, Integer sort, Long apiClientId) {
		if (StrUtils.isNULL(title)) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "标题");
		}
		if (StrUtils.isNULL(image)) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "图片地址");
		}
		if (StrUtils.isNULL(url)) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "指向链接");
		}
		if (startDate == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "开始时间");
		}
		if (endDate == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "结束时间");
		}
		if (sort == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "序号");
		}

		Date _startDate = DateUtils.getDateFromTimeStamp(String.valueOf(startDate), "yyyy-MM-dd");
		Date _endDate = DateUtils.getDateFromTimeStamp(String.valueOf(endDate), "yyyy-MM-dd");
		Advertisement advertisement = new Advertisement();
		advertisement.setTitle(title);
		advertisement.setDescription(description);
		advertisement.setEndDate(_endDate);
		advertisement.setImage(image);
		advertisement.setStartDate(_startDate);
		advertisement.setStatus(0);
		advertisement.setUrl(url);
		advertisement.setSort(sort);
		advertisement.setApiClientId(apiClientId);

		advertisementRepository.save(advertisement);
		return advertisement;
	}

	@Override
	public Advertisement updateAdvertisement(Long id, String title, String description, String image, String url,
			Long startDate, Long endDate, Integer status, Integer sort, Long apiClientId) {
		if (id == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "id");
		}
		Advertisement advertisement = (Advertisement) advertisementRepository.findById(id, apiClientId);
		if (advertisement == null) {
			throw new ServiceException(StatusHouse.AD_NOT_FOUND);
		}
		if (StrUtils.isNotNULL(title)) {
			advertisement.setTitle(title);
		}
		if (StrUtils.isNotNULL(description)) {
			advertisement.setDescription(description);
		}
		if (StrUtils.isNotNULL(image)) {
			advertisement.setImage(image);
		}
		if (StrUtils.isNotNULL(url)) {
			advertisement.setUrl(url);
		}
		if (startDate != null) {
			Date _startDate = DateUtils.getDateFromTimeStamp(String.valueOf(startDate), "yyyy-MM-dd");
			advertisement.setStartDate(_startDate);
		}
		if (endDate != null) {
			Date _endDate = DateUtils.getDateFromTimeStamp(String.valueOf(endDate), "yyyy-MM-dd");
			advertisement.setEndDate(_endDate);
		}
		if (status != null) {
			advertisement.setStatus(status);
		}
		if (sort != null) {
			advertisement.setSort(sort);
		}
		advertisementRepository.update(advertisement);
		return advertisement;
	}

	@Override
	public Advertisement findAdvertisementById(Long id, Long apiClientId) {
		if (id == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "id");
		}
		Advertisement advertisement = (Advertisement) advertisementRepository.findById(id, apiClientId);
		return advertisement;
	}

	@Override
	public void deleteAdvertisements(String ids, Long apiClientId) {
		if (StrUtils.isNULL(ids)) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "id");
		}
		if (!ids.contains(",")) {
			Advertisement advertisement = (Advertisement) advertisementRepository.findById(Long.valueOf(ids), apiClientId);
			if(advertisement != null){
				advertisementRepository.deleteById(Long.valueOf(ids));
			}
		} else {
			String[] _ids = ids.split(",");
			Long[] _ids_ = new Long[_ids.length];
			for (int i = 0; i < _ids.length; i++) {
				Advertisement advertisement = (Advertisement) advertisementRepository.findById(Long.valueOf(_ids[i]), apiClientId);
				if(advertisement != null){
					_ids_[i] = Long.valueOf(_ids[i]);
				}
			}
			if (_ids.length > 0) {
				advertisementRepository.delete(_ids_);
			}
		}

	}

}
